<template>
  <div class="login">
    <!-- 头部 -->
    <sHeader :name="state.type == 'login' ? '登录' : '注册'" :back="'/home'"></sHeader>
    <!-- 图片 -->
    <img class="logo" src="https://s.yezgea02.com/1604045825972/newbee-mall-vue3-app-logo.png" alt="" />
    <!-- 表单 -->
    <div v-if="state.type == 'login'" class="login-body login">
      <van-form @submit="onSubmit">
        <van-field v-model="state.username" name="username" :rules="[{ required: true, message: '请填写用户名' }]" label="用户名" placeholder="用户名" />
        <van-field v-model="state.password" name="password" :rules="[{ required: true, message: '请填写密码' }]" label="密码" placeholder="密码" />
        <van-field v-model="state.verify" label="验证码" placeholder="输入验证码">
          <template #button>
            <vue-img-verify ref="verifyRef"></vue-img-verify>
          </template>
        </van-field>
        <div style="margin: 16px">
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <van-button round block color="#7232dd" native-type="submit" size="large">登录</van-button>
        </div>
      </van-form>
    </div>
    <!-- // 注册表单 -->
    <div v-else class="login-body register">
      <van-form @submit="onSubmit">
        <van-field v-model="state.username1" name="username1" :rules="[{ required: true, message: '请填写用户名' }]" label="用户名" placeholder="请输入用户名" />
        <van-field v-model="state.password1" name="password1" :rules="[{ required: true, message: '请填写密码' }]" label="密码" placeholder="密码" />
        <van-field v-model="state.verify" label="验证码" placeholder="验证码">
          <template #button>
            <vue-img-verify ref="verifyRef"></vue-img-verify>
          </template>
        </van-field>

        <div style="margin: 16px">
          <div class="link-login" @click="toggle('login')">已有登录账号</div>
          <van-button round block color="#7232dd" native-type="submit" size="large">注册</van-button>
        </div>
      </van-form>
    </div>

    <!-- 登录按钮 -->
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import sHeader from "@/components/simpleHeader.vue";
import vueImgVerify from "@/components/VueImageVerify.vue";
import { login, register } from "@/service/user.js";
import { setLocal } from "@/common/js/utils"; // 引入封装好的本地存储方法
import md5 from "js-md5"; // 引入md5加密

const verifyRef = ref(null); // 验证码组件
const state = reactive({
  username: "",
  password: "",
  username1: "",
  password1: "",
  verify: "", // 输入的验证码
  type: "login",
  imgCode: "", // 图片验证码
});
//切换登录和注册模式
const toggle = (type) => {
  state.type = type;
  state.verify = ""; // 清空验证码
};
// 登录提交
const onSubmit = async (values) => {
  state.imgCode = verifyRef.value.state.imgCode || ""; // 获取图片验证码
  //校验用户输入验证码和验证码的值是否正确
  if (state.verify.toLowerCase() != state.imgCode.toLowerCase()) {
    showFailToast("验证码有误");
    return;
  }
  if (state.type == "login") {
    console.log(values, "登录提交");

    // 登录
    const params = {
      loginName: values.username,
      passwordMd5: md5(values.password), // 密码加密
    };
    const { data } = await login(params);
    console.log(data, "登录返回数据");
    //缓存token到本地
    setLocal("token", data);
    // 需要刷新页面，否则 axios.js 文件里的 token 不会被重置
    window.location.href = "/";
  } else {
    await register({
      loginName: values.username1,
      password: values.password1,
    });
    showSuccessToast("注册成功");
    state.type = "login"; //切换到登录模式
    state.verify = "";
  }
};
</script>
<style lang="less" scoped>
.login {
  .logo {
    width: 120px;
    height: 120px;
    display: block;
    margin: 80px auto 20px;
  }
  .login-body {
    padding: 0 20px;
  }
  .login {
    .link-register {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: block;
    }
  }
  .register {
    .link-login {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: block;
    }
  }
  .verify-bar-area {
    margin-top: 24px;
    .verify-left-bar {
      border-color: #1baeae;
    }
    .verify-move-block {
      background-color: #1baeae;
      color: #fff;
    }
  }
  .verify {
    > div {
      width: 100%;
    }
    display: flex;
    justify-content: center;
    .cerify-code-panel {
      margin-top: 16px;
    }
    .verify-code {
      width: 40% !important;
      float: left !important;
    }
    .verify-code-area {
      float: left !important;
      width: 54% !important;
      margin-left: 14px !important;
      .varify-input-code {
        width: 90px;
        height: 38px !important;
        border: 1px solid #e9e9e9;
        padding-left: 10px;
        font-size: 16px;
      }
      .verify-change-area {
        line-height: 44px;
      }
    }
  }
}
</style>
